package com.alibaba.vo

import java.util.Base64
import javax.crypto.spec.SecretKeySpec

//import sun.misc.BASE64Encoder
import javax.crypto.Mac

object HMACSha1Encode {
  /*
   * 计算 HMAC-SHA1
   */
  def HMACSha1(
    data: String,
    key: String = "sf(2342@**^^fseww)723465164fsdf sjfs @324234&^"): String = {
    try {
      val signingKey = new SecretKeySpec(key.getBytes("utf-8"), "HmacSHA1")
      val mac = Mac.getInstance("HmacSHA1")
      mac.init(signingKey)
      val rawHmac = mac.doFinal(data.getBytes("utf-8"))
      //(new BASE64Encoder).encode(rawHmac)
      new String(Base64.getEncoder.encode(rawHmac), "utf-8")
    } catch {
      case e: Exception =>
        throw new Error("Failed to generate HMAC : " + e.getMessage)
    }
  }
}
